<?php

/**
 * HTML生成控制器类
 * 
 * @package    01CMS
 * @subpackage admin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class HtmlController extends Controller
{
    public $title = '更新内容';
    public $Db;
    public $Model;
    public $all = FALSE;

    function __construct ()
    {
        parent::__construct();
        
        $User = model('User');
        
        if (! $User->isLogined())
        {
            redirect('admin/login?url=' . getCurUrl());
        }
        
        if (! $User->checkAuth())
        {
            exit('您的权限不足');
        }
        
        if(!defined('staticHtml'))
        {
            define('staticHtml',TRUE);
        }
        
        $this->Db = model('Db');
        $this->Model = model('Html');
    }

    function index ()
    {
        $this->category = model('Category')->getChildren (0, 1);
        
        $this->Load->view('smarty');
    }

    function update ($action = '', $id = 0)
    {
        $this->Load->view('smarty');
        
        ini_set('max_execution_time', 0);
        ob_implicit_flush(1);
        
        if ($action == 'homepage')
        {
            $this->homepage();
        }
        else if ($action == 'single')
        {
            $this->single();
        }
        else if ($action == 'category')
        {
            $this->category($id);
        }
        else if ($action == 'archive')
        {
            $this->archive();
        }
        else if ($action == 'all')
        {
            $this->all = TRUE;
            $this->homepage();
            $this->single();
            $this->category($id);
            $this->archive();
            $time = $this->getTime();
            $this->output('tip', '一键更新', 'html');
            $this->output('status', "更新完毕，共用去{$time}分钟。【<a href='" . BOOT_URL . "/html/index'>返回</a>】", 'html');
        }

    }

    function homepage ()
    {
        $this->output('tip', '更新首页', 'html');
        $this->progress(20);
        $this->Model->indexHtml('write');
        $this->progress(100);
        if ($this->all)
        {
            $this->output('info', "首页更新完毕。");
            $this->output('status', "开始更新单页文档...", 'html');
        }
        else
        {
            $this->output('status', "成功更新首页【<a href='" . BOOT_URL . "/html/index'>返回</a>】", 'html');
        }
    }

    function category ($id = 0)
    {
        $this->output('tip', '更新栏目', 'html');
        
        $category = model('Category')->getData();
        
        if ($id > 0)
        {
            $this->progress(20);
            $this->Model->writeCategoryHtml($id);
            $this->progress(100);
            
            $this->output('status', "成功更新栏目：{$category[$id]['name']}【<a href='" . BOOT_URL . "/html/index'>返回</a>】", 'html');
        }
        else
        {
            
            $count = count($category['ids']);
            
            $i = 1;
            
            foreach ($category['ids'] as $id)
            {
                $this->Model->writeCategoryHtml($id);
                $percent = ceil($i / $count * 100);
                $time = $this->getTime();
                $this->progress($percent);
                $this->output('status', "正在更新栏目：已完成{$percent}% （共有{$count}个栏目，已更新{$i}个，已用去{$time}分钟）", 'html');
                //sleep(1);
                $i ++;
            }
            if ($this->all)
            {
                $this->output('info', "<br />栏目更新完毕（共更新了{$count}个栏目）。");
                $this->output('status', "开始更新文档...", 'html');
            }
            else
            {
                $this->output('status', "全部栏目更新完毕，共更新了{$count}个栏目，用去{$time}分钟。【<a href='" . BOOT_URL . "/html/index'>返回</a>】", 'html');
            }
        }
    }

    function archive ()
    {
        $data = $this->Load->query('limitTime,startTime,endTime,limitId,startId,endId');
        
        $where = 'where pass = 1 ';
        
        if ($data['limitTime'] > 0)
        {
            $startTime = strtotime($data['startTime']);
            $endTime = strtotime($data['endTime']);
            
            if ($startTime > 0)
            {
                $where .= ' and addTime >' . $startTime;
            }
            
            if ($endTime > 0)
            {
                $where .= ' and addTime <' . $endTime;
            }
        }
        
        if ($data['limitId'] > 0)
        {
            if ($data['startId'] > 0)
            {
                $where .= ' and id >' . $data['startId'];
            }
            
            if ($data['endId'] > 0)
            {
                $where .= ' and id <' . $data['endId'];
            }
        }
        
        $count = $this->Db->getRow("select count(*) as num from @@__archive_archive {$where}");
        $count = $count->num;
        
        $this->output('tip', '更新文档', 'html');
        
        $this->Db->query("select id from @@__archive_archive {$where}", 'updateArchive');
        
        $i = 1;
        
        while ($row = $this->Db->getNextRow('updateArchive'))
        {
            $this->Model->writeArchiveHtml($row->id);
            $percent = ceil($i / $count * 100);
            $time = $this->getTime();
            $this->progress($percent);
            $this->output('status', "正在更新文档：已完成{$percent}% （共有{$count}篇文档，已更新{$i}篇，已用去{$time}分钟）", 'html');
            //sleep(1);
            $i ++;
        }
        
        if ($this->all)
        {
            $this->output('info', "<br />文档更新完毕（共更新了{$count}篇文档）。");
            $this->output('status', "文档更新完毕", 'html');
        }
        else
        {
            $this->output('status', "更新完毕，共更新了{$count}篇文档，用去{$time}分钟  。【<a href='" . BOOT_URL . "/html/index'>返回</a>】", 'html');
        }
        //sleep(1);
    }

    function single ()
    {
        $this->output('tip', '更新单页文档', 'html');
        $this->progress(20);
        $this->output('status', "正在更新单页文档...", 'html');
        $count = $this->Model->writeAllSingleHtml();
        $this->progress(100);
        if ($this->all)
        {
            $this->output('info', "<br />单页文档更新完毕（共更新了{$count}篇单页文档）。");
            $this->output('status', "开始更新栏目...", 'html');
        }
        else
        {
            $this->output('status', "成功更新了{$count}篇单页文档。【<a href='" . BOOT_URL . "/html/index'>返回</a>】", 'html');
        }
    }

    private function getTime ()
    {
        $time = (time() - TIME) / 60;
        $time = round($time, 2);
        
        return $time;
    }

    private function progress ($percent)
    {
        if ($percent == 100)
        {
            echo "<script>$('#progressBar').css({'width':'100%','background':'url(" . PUBLIC_DIR . "/common/image/progressbarstatic.gif)'});</script>";
        }
        else
        {
            echo "<script>$('#progressBar').css({'width':'{$percent}%'});</script>";
        }
        
        @ob_flush();
        @flush();
    }

    private function output ($id, $content, $type = 'append')
    {
        echo '<script>$("#' . $id . '").' . $type . '("' . $content . '");</script>';
        @ob_flush();
        @flush();
    }
}
